﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication2.Model
{
    class Cabang
    {
        int id;
        string nama;
        string alamat;
        bool stok_saja;

        public Cabang()
        {

        }

        public Cabang(string _Nama)
        {
            Database db = new Database();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            string sql = "SELECT * FROM cabang WHERE nama = '" + _Nama + "'";

            db.Open();
            ds = db.ExecuteQuery(sql);
            dt = ds.Tables[0];
            DataRow dr = dt.Rows[0];

            id = (int)dr[0];
            nama = (string)dr[1];
            alamat = (string)dr[2];
            stok_saja = (bool)dr[4];

            db.Close();
        }

        public ArrayList daftarStokCabang(int _stoc_saja)
        {
            return null;
        }

        public static ArrayList daftarCabang()
        {
            ArrayList a = new ArrayList();
            Database db = new Database();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] o = new object[2];
            string sql = "SELECT * FROM cabang";

            db.Open();
            ds = db.ExecuteQuery(sql);
            dt = ds.Tables[0];
            DataRow dr;
            for (int i = 0; i < dt.Rows.Count; ++i)
            {
                dr = dt.Rows[i];

                o[0] = dr[0];
                o[1] = dr[1];
                a.Add(o.Clone());
            }

            db.Close();

            return a;
        }

        public void addCabang()
        {
            Database db = new Database();
            string sql = "INSERT INTO cabang(nama, alamat, stok_saja)) VALUES ('" + nama + "','" + alamat + "','" + stok_saja.ToString() + "')";
            db.Open();
            db.ExecuteNonQuery(sql);
            db.Close();

        }

        public void addCabang(string _nama, string _alamat, bool _stok)
        {
            Database db = new Database();
            string sql = "INSERT INTO cabang(nama, alamat, stok_saja)) VALUES ('" + _nama + "','" + _alamat + "','" + _stok.ToString() + "')";
            nama = _nama;
            alamat = _alamat;
            stok_saja = _stok;
            db.Open();
            db.ExecuteNonQuery(sql);
            db.Close();

        }

        public void updateCabang()
        {
        }

        public void deleteCabang()
        {
        }

    }
}
